package grandPrize

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/base-common/auth"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/grandprize"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateGrandPrizeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 修改
func NewUpdateGrandPrizeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateGrandPrizeLogic {
	return &UpdateGrandPrizeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateGrandPrizeLogic) UpdateGrandPrize(req *types.UpdateGrandPrizeReq) (resp *types.Empty, err error) {
	if req.Id == 0 {
		return nil, code.ParamsInvalid
	}

	userId := auth.GetTokenUid(l.ctx)
	if userId == 0 {
		return nil, code.NotLogin
	}

	if req.Data.MultiplierStart > req.Data.MultiplierEnd || req.Data.OrderAmountStart > req.Data.OrderAmountEnd {
		return nil, code.ParamsInvalid
	}

	u, err := l.svcCtx.SettingService.GrandPrize.UpdateGrandPrize(l.ctx, &grandprize.UpdateGrandPrizeReq{
		Id: req.Id,
		Data: &grandprize.AddGrandPrizeReq{
			MerchantId:       1,
			MultiplierStart:  float32(req.Data.MultiplierStart),
			MultiplierEnd:    float32(req.Data.MultiplierEnd),
			OrderAmountStart: float32(req.Data.OrderAmountStart),
			OrderAmountEnd:   float32(req.Data.OrderAmountEnd),
			Bonus:            float32(req.Data.Bonus),
			Sort:             req.Data.Sort,
			SendDay:          req.Data.SendDay,
			Status:           rpc.StatusType(req.Data.Status),
			Operator:         "",
			OperatorId:       userId,
		},
	})

	if err != nil {
		logc.Error(l.ctx, "UpdateGrandPrize err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	resp = &types.Empty{}
	return
}
